package reflect;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Scanner;

/**
 * 利用反射机制调用方法
 */
public class reflectDemo4 {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Person p=new Person();
        p.sayHello();
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个类名:");
        String className=scanner.nextLine();
        System.out.println("请输入一个方法名");
        String methodName=scanner.nextLine();

       // Class cls=Class.forName("reflect.Person");
        Class cls=Class.forName(className);
        Object o=cls.newInstance();
        //Method method=cls.getMethod("sayHello");
        Method method=cls.getMethod(methodName);
        method.invoke(o);


    }
}
